﻿using UnityEngine;
using System.Collections;

public class Popup : MonoBehaviour {    
    //UIPanel m_UIPanel;

    GameObject m_EventRecieaver;
    string m_FuncName;
    object m_Param;
    protected bool isNeedClose = true;
    bool m_AbleControl = true;

    virtual public void AwakeInit()
    {
    }

    void Awake()
    {        
        AwakeInit();
    }

    void Start()
    {
        StartInit();
    }

    virtual public void StartInit()
    {
    }

    //IEnumerator StartFunc(float time)
    //{
    //    yield return new WaitForSeconds(time);
    //    m_AbleControl = true;
    //}

    virtual public void OnDestroyFunc()
    { 
    }

    void OnDestroy()
    {
        OnDestroyFunc();        
    }

    public void SetHandler(GameObject eventRecieaver, string funcName, object Param)
    {
        m_EventRecieaver = eventRecieaver;
        m_FuncName = funcName;
        m_Param = Param;
    }

    void OnOk(GameObject obj)
    {
        if (m_AbleControl)
        {
            if (m_EventRecieaver != null && !string.IsNullOrEmpty(m_FuncName))
            {
                if (m_Param == null)
                    m_EventRecieaver.SendMessage(m_FuncName, SendMessageOptions.DontRequireReceiver);
                else
                    m_EventRecieaver.SendMessage(m_FuncName, m_Param, SendMessageOptions.DontRequireReceiver);
            }

            OnOkFunc(obj);

            if (isNeedClose)
                PopupManager.PopupPop(gameObject);
            isNeedClose = true;
        }
    }

    public virtual void OnOkFunc(GameObject obj)
    {
    }

    void OnX(GameObject obj)
    {
        if (m_AbleControl)
        {
            OnXFunc(obj);

            if (isNeedClose)
                PopupManager.PopupPop(gameObject);
            isNeedClose = true;
        }
    }

    public virtual void OnXFunc(GameObject obj)
    {
        
    }
}
